package com.bei_en.teacher.admin.controller.questions;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.questions.bean.QuestionsDatabase;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsDatabaseCondition;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsDatabaseVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.service.questions.QuestionsDatabaseService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * 
 * @author Guocg
 * @date 2016-8-20 23:58:53
 */
@Controller
@RequestMapping("/api/Questions")
public class QuestionsDatabaseAPPController extends BaseController {

	@Autowired
	QuestionsDatabaseService questionsDatabaseService;

	@RequestMapping("/questionsDatabase")
	@ResponseBody
	public JsonResult questionsDatabase(Model model,
			@Valid QuestionsDatabaseCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			List<QuestionsDatabaseVo> list = new ArrayList<QuestionsDatabaseVo>();
			list = questionsDatabaseService.findQuestionsDatabasePage(vo);
			
			JSONArray array = new JSONArray();
			JSONObject object = null;
			QuestionsDatabaseVo questionsDatabaseVo = null;
			for (int i = 0; i < list.size(); i++) {
				object = new JSONObject();
				questionsDatabaseVo = list.get(i);
				object.put("Id", questionsDatabaseVo.getId());
				object.put("version",  questionsDatabaseVo.getCreateTime().getTime()+"");
				object.put("type", questionsDatabaseVo.getType());
//				object.put("content", questionsDatabaseVo.getContent());
				object.put("content", "");
				object.put("createTime", questionsDatabaseVo.getCreateTime().getTime());
			}
			return new JsonSuccessResult(session.getId(), object);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/questionsDatabaseAdd")
	@ResponseBody
	public JsonResult questionsDatabaseAdd(Model model,
			@Valid QuestionsDatabaseCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {

			QuestionsDatabaseVo questionsDatabase = new QuestionsDatabaseVo();
			questionsDatabaseService.insert(questionsDatabase);
			return new JsonSuccessResult(session.getId(), "成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/questionsDatabaseEdit")
	public JsonResult questionsDatabaseEdit(Model model,
			@Valid QuestionsDatabaseCondition vo, BindingResult br,
			HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			QuestionsDatabaseVo questionsDatabase = new QuestionsDatabaseVo();

			questionsDatabaseService.updateSelective(questionsDatabase);

			return new JsonSuccessResult(session.getId(), "成功");

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

}
